package at.ac.tuwien.mg.service;

import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

import at.ac.tuwien.mg.bean.Offer;

//TODO: check if class is needed/used
public class RankOffers implements JavaDelegate {

	@Override
	public void execute(DelegateExecution de) throws Exception {
		System.out.println("RANK OFFER CALLED");
		
		@SuppressWarnings("unchecked")
		Vector<Offer> offers = (Vector<Offer>) de.getVariable("offers");
		
		Comparator<Offer> comparator = new Comparator<Offer>() {
			@Override
			public int compare(Offer o1, Offer o2) {
				double score1 = o1.getTotal();
				double score2 = o2.getTotal();
				if(score1 < score2) {
					return -1;
				}
				else if(score1 == score2) {
					return 0;
				}
				else {
					return 1;
				}
			}
		};
		
		Collections.sort(offers, comparator);
		de.setVariable("offers", offers);
		
	}

}
